package com.book.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.book.common.annotation.Log;
import com.book.common.annotation.group.InsertGroup;
import com.book.common.annotation.group.UpdateGroup;
import com.book.common.core.controller.BaseController;
import com.book.common.core.domain.AjaxResult;
import com.book.common.enums.BusinessType;
import com.book.common.utils.DateUtils;
import com.book.common.utils.StringUtils;
import com.book.system.domain.Book;
import com.book.system.domain.BookCategory;
import com.book.system.service.IBookCategoryService;
import com.book.system.service.IBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author Bigboss
 * @create 2024-10-19 20:31
 */
@Api(tags = "书籍分类管理")
@RestController
@RequestMapping("/system/book/category")
public class SysBookCategoryController extends BaseController {

    private final IBookCategoryService bookCategoryService;

    private final IBookService bookService;

    public SysBookCategoryController(IBookCategoryService bookCategoryService, IBookService bookService) {
        this.bookCategoryService = bookCategoryService;
        this.bookService = bookService;
    }

    /**
     * 获取书籍分类列表
     * @return
     */
    @ApiOperation(value = "轮播图列表，权限：system:bookCategory:list,system:book:add,system:book:edit(有其一即可)")
    @PreAuthorize("@ss.hasAnyPermi('system:bookCategory:list,system:book:add,system:book:edit')")
    @GetMapping("/list")
    public AjaxResult list()
    {
        return success(bookCategoryService.list(new LambdaQueryWrapper<BookCategory>()
                .orderByAsc(BookCategory::getCategorySort)));
    }


    /**
     * 新增书籍分类
     */
    @ApiOperation(value = "新增书籍分类，权限：system:bookCategory:add")
    @PreAuthorize("@ss.hasPermi('system:bookCategory:add')")
    @Log(title = "书籍分类管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated({InsertGroup.class}) @RequestBody BookCategory entity)
    {

        entity.setCreateBy(getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        return toAjax(bookCategoryService.save(entity));
    }

    /**
     * 修改书籍分类
     */
    @ApiOperation(value = "修改轮播图，权限：system:bookCategory:edit")
    @PreAuthorize("@ss.hasPermi('system:bookCategory:edit')")
    @Log(title = "书籍分类管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated({UpdateGroup.class}) @RequestBody BookCategory entity)
    {
        entity.setUpdateBy(getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        return toAjax(bookCategoryService.updateById(entity));
    }

    /**
     * 删除书籍分类
     */
    @ApiOperation(value = "删除轮播图，权限：system:bookCategory:remove")
    @PreAuthorize("@ss.hasPermi('system:bookCategory:remove')")
    @Log(title = "书籍分类管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable String id)
    {
        //查询分类下是否有数据
        Book book = bookService.getOne(new LambdaQueryWrapper<Book>().eq(Book::getCategoryId, id).last("LIMIT 1"));
        if(StringUtils.isNotNull(book)){
            return error("该分类下有书籍，无法删除");
        }
        return toAjax(bookCategoryService.removeById(id));
    }
}
